//
//  WmcTimeLabel.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/7.
//

import RxSwift
import UIKit
import YPBedrock

public extension WmcDateLabel {
    /// 日期格式化样式
    enum Format: String {
        case yyyy_MM_dd = "yyyy-MM-dd"
        case cn_yyyy_MM_dd = "yyyy年MM月dd日"
        case yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm"
        case yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"

        case yyyy_MM_dd_week = "yyyy-MM-dd WEEK"
        case yyyy_MM_dd_enter_week = "yyyy-MM-dd\nWEEK"
        case yyyy_MM_dd_week_HH_mm = "yyyy-MM-dd WEEK HH:mm"
        case yyyy_MM_dd_HH_mm_week = "yyyy-MM-dd HH:mm WEEK"
        case yyyy_MM_dd_HH_mm_ss_week = "yyyy-MM-dd HH:mm:ss WEEK"

        case yyyy_P_MM_P_dd_week = "yyyy.MM.dd WEEK"
        case yyyy_P_MM_P_dd_HH_mm_ss_week = "yyyy.MM.dd HH:mm:ss WEEK"
        
        case week = "WEEK"
        case week_HH_mm = "WEEK HH:mm"
        case week_enter_yyyy_MM_dd = "WEEK\nyyyy-MM-dd"

        case zh_MM = "MM月"
        case dd

        case HH_mm = "HH:mm"
        case HH_mm_ss = "HH:mm:ss"

        case zh_ampm = "a"
    }
}

// MARK: - [WmcDateLabel]

open class WmcDateLabel: WmcBaseSystemFieldLabel {
    // Open

    open var prefix: String?

    /// 显示格式
    open var format: Format = .yyyy_MM_dd_week_HH_mm {
        didSet { showText() }
    }

    /// 配置
    override open func config() {
        networkOb = WmcDateManager.shared.networkDate.subscribe(onNext: { [weak self] date in
            guard let weakSelf = self,
                  let _date = date,
                  !weakSelf.styleMode.isStatic else { return }
            weakSelf.updateDate(_date)
//            WmcDebug("时间更新: \(_date.wmc_toString(format: "HH:mm:ss"))")
        })
        text = "获取时间中..."
    }

    // Internal

    /// 当前时间
    var currentDate: Date?

    var networkOb: Disposable!

    /// 子类获取时间的方法
    func updateDate(_ date: Date) {
        YPGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.currentDate = date
            weakSelf.showText()
        }
    }

    /// 显示文字
    func showText() {
//        // 检查网络权限
//        WmcAuthorizedManager.network { status in
//            if status.info.isAvailable { // 有
//                <#code#>
//            }else{
//
//            }
//        }

        // 直接转format
        guard let currentDate = currentDate else { return }
        var formatStr = format.rawValue
        if let prefix = prefix {
            formatStr = "\(prefix)\(formatStr)"
        }
        if formatStr.contains("WEEK") { // 带星期
            let weekStr = currentDate.yp.weekNameCN
            formatStr = formatStr.replacingOccurrences(of: "WEEK", with: weekStr)
        }

        let format = DateFormatter()
        format.timeZone = TimeZone.YP.CreateOptional(identifier: .gmt(.GMT))
        format.locale = Locale(identifier: "zh_CN")
        format.calendar = Calendar(identifier: .iso8601)
        format.dateFormat = formatStr
        format.amSymbol = "上午"
        format.pmSymbol = "下午"
        let str = format.string(from: currentDate)
        text = str
    }
}
